package com.bysc.cmsservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bysc.cmsservice.entity.CmsProject;
import com.bysc.cmsservice.service.CmsProjectService;
import com.bysc.commonutils.Results;
import com.bysc.commonutils.constant.MessageConstant;
import com.bysc.commonutils.vo.BaseQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 项目表 前端控制器
 * </p>
 *
 * @author kkk
 * @since 2024-10-24
 */
@RestController
@RequestMapping("/cmsservice/cms-project")
@Api(tags = "项目管理")
public class CmsProjectController {

    @Autowired
    private CmsProjectService cmsProjectService;

    @ApiOperation("新增项目")
    @PostMapping("addProject")
    public Results addProject(@RequestBody CmsProject cmsProject) {
        cmsProjectService.save(cmsProject);
        return Results.ok().data("project", cmsProject).message(MessageConstant.ADD_PROJECT_SUCCESS);
    }

    @ApiOperation("根据id查找项目")
    @GetMapping("queryProjectById/{id}")
    public Results queryProjectById(@PathVariable String id) {
        CmsProject project = cmsProjectService.getById(id);
        return Results.ok().data("project", project).message(MessageConstant.QUERY_PROJECT_SUCCESS);
    }

    @ApiOperation("更新项目")
    @PostMapping("updateProject")
    public Results updateProject(@RequestBody CmsProject cmsProject) {
        cmsProjectService.updateById(cmsProject);
        return Results.ok().message(MessageConstant.UPDATE_PROJECT_SUCCESS);
    }

    @ApiOperation("分页查询项目")
    @PostMapping("getProjectList/{page}/{limit}")
    public Results getProjectList(
            @ApiParam(name = "page", value = "查询页数", required = true) @PathVariable Integer page,
            @ApiParam(name = "limit", value = "查询数量", required = true) @PathVariable Integer limit,
            @RequestBody(required = false) BaseQueryVo queryVo
    ) {
        Page<CmsProject> pageParam = new Page<>(page, limit);
        LambdaQueryWrapper<CmsProject> wrapper = new LambdaQueryWrapper<>();
        if (queryVo != null) {
            String id = queryVo.getId();
            String value = queryVo.getValue();

            if (!StringUtils.isEmpty(id)) {
                wrapper.eq(CmsProject::getId, id);
            }
            if (!StringUtils.isEmpty(value)) {
                wrapper.like(CmsProject::getMember, value).or()
                        .like(CmsProject::getTeacher, value).or()
                        .like(CmsProject::getCompetition, value).or()
                        .like(CmsProject::getCurrentProgress, value).or()
                        .like(CmsProject::getFutureProgress, value).or()
                        .like(CmsProject::getDescribe, value).or()
                        .like(CmsProject::getProjectName, value).or()
                ;
            }
            wrapper.orderByDesc(CmsProject::getGmtModified);
        }
        IPage<CmsProject> cmsProjectIPage = cmsProjectService.page(pageParam, wrapper);
        return Results.ok().data("list", cmsProjectIPage).message(MessageConstant.QUERY_PROJECT_SUCCESS);
    }

    @ApiOperation("根据id删除项目")
    @DeleteMapping("deleteProjectById/{id}")
    public Results deleteProjectById(@PathVariable String id) {
        cmsProjectService.removeById(id);
        return Results.ok().message(MessageConstant.DELETE_PROJECT_SUCCESS);
    }
}
